﻿using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Localization;
using Nop.Core.Domain.Topics;
using Nop.Core.Domain.ShareBuy;
using Nop.Services.Localization;
using Nop.Services.Seo;
using Nop.Web.Models.Catalog;
using Nop.Web.Models.Common;
using Nop.Web.Models.Topics;
using Nop.Web.Models.ShareBuy;
using AutoMapper;

namespace Nop.Web.Extensions
{
    public static class MappingExtensions
    {
        //category
        public static CategoryModel ToModel(this Category entity)
        {
            if (entity == null)
                return null;

            var model = new CategoryModel()
            {
                Id = entity.Id,
                Name = entity.GetLocalized(x => x.Name),
                Description = entity.GetLocalized(x => x.Description),
                MetaKeywords = entity.GetLocalized(x => x.MetaKeywords),
                MetaDescription = entity.GetLocalized(x => x.MetaDescription),
                MetaTitle = entity.GetLocalized(x => x.MetaTitle),
                SeName = entity.GetSeName(),
            };
            return model;
        }

        //manufacturer
        public static ManufacturerModel ToModel(this Manufacturer entity)
        {
            if (entity == null)
                return null;

            var model = new ManufacturerModel()
            {
                Id = entity.Id,
                Name = entity.GetLocalized(x => x.Name),
                Description = entity.GetLocalized(x => x.Description),
                MetaKeywords = entity.GetLocalized(x => x.MetaKeywords),
                MetaDescription = entity.GetLocalized(x => x.MetaDescription),
                MetaTitle = entity.GetLocalized(x => x.MetaTitle),
                SeName = entity.GetSeName(),
            };
            return model;
        }

        //language
        public static LanguageModel ToModel(this Language entity)
        {
            if (entity == null)
                return null;

            var model = new LanguageModel()
            {
                Id = entity.Id,
                Name = entity.Name,
                FlagImageFileName = entity.FlagImageFileName,
            };
            return model;
        }


        //currency
        public static CurrencyModel ToModel(this Currency entity)
        {
            if (entity == null)
                return null;

            var model = new CurrencyModel()
            {
                Id = entity.Id,
                Name = entity.GetLocalized(x => x.Name),
            };
            return model;
        }

        //product
        public static ProductModel ToModel(this Product entity)
        {
            if (entity == null)
                return null;

            var model = new ProductModel()
            {
                Id = entity.Id,
                Name = entity.GetLocalized(x => x.Name),
                ShortDescription = entity.GetLocalized(x => x.ShortDescription),
                FullDescription = entity.GetLocalized(x => x.FullDescription),
                MetaKeywords = entity.GetLocalized(x => x.MetaKeywords),
                MetaDescription = entity.GetLocalized(x => x.MetaDescription),
                MetaTitle = entity.GetLocalized(x => x.MetaTitle),
                SeName = entity.GetSeName(),
            };
            return model;
        }

        //address
        public static AddressModel ToModel(this Address entity)
        {
            if (entity == null)
                return null;

            var model = new AddressModel()
            {
                Id = entity.Id,
                FirstName = entity.FirstName,
                LastName = entity.LastName,
                Email = entity.Email,
                Company = entity.Company,
                CountryId = entity.CountryId,
                CountryName = entity.Country != null ? entity.Country.GetLocalized(x => x.Name) : null,
                StateProvinceId = entity.StateProvinceId,
                StateProvinceName = entity.StateProvince != null ? entity.StateProvince.GetLocalized(x => x.Name) : null,
                City = entity.City,
                Address1 = entity.Address1,
                Address2 = entity.Address2,
                ZipPostalCode = entity.ZipPostalCode,
                PhoneNumber = entity.PhoneNumber,
                FaxNumber = entity.FaxNumber,
            };
            return model;
        }
        public static Address ToEntity(this AddressModel model)
        {
            if (model == null)
                return null;

            var entity = new Address();
            return ToEntity(model, entity);
        }
        public static Address ToEntity(this AddressModel model, Address destination)
        {
            if (model == null)
                return destination;

            destination.Id = model.Id;
            destination.FirstName = model.FirstName;
            destination.LastName = model.LastName;
            destination.Email = model.Email;
            destination.Company = model.Company;
            destination.CountryId = model.CountryId;
            destination.StateProvinceId = model.StateProvinceId;
            destination.City = model.City;
            destination.Address1 = model.Address1;
            destination.Address2 = model.Address2;
            destination.ZipPostalCode = model.ZipPostalCode;
            destination.PhoneNumber = model.PhoneNumber;
            destination.FaxNumber = model.FaxNumber;

            return destination;
        }

        //topics
        public static TopicModel ToModel(this Topic entity)
        {
            if (entity == null)
                return null;

            var model = new TopicModel()
            {
                Id = entity.Id,
                SystemName = entity.SystemName,
                IncludeInSitemap = entity.IncludeInSitemap,
                IsPasswordProtected = entity.IsPasswordProtected,
                Title = entity.GetLocalized(x => x.Title),
                Body = entity.GetLocalized(x => x.Body),
                MetaKeywords = entity.GetLocalized(x => x.MetaKeywords),
                MetaDescription = entity.GetLocalized(x => x.MetaDescription),
                MetaTitle = entity.GetLocalized(x => x.MetaTitle),
            };
            return model;
        }

        //shareBuy
        public static ShareBuyItem ToEntity (this ShareBuyModel model)
        {
            if (model == null)
                return null;
            var entity = new ShareBuyItem();
            entity.CreatedOnUtc = model.CreatedOnUtc;
            entity.CustomerId = model.CustomerId;
            entity.DefaultPictureId = model.DefaultPictureModel.Id;
            entity.Description = model.Description;
            entity.Deleted = model.Deleted;
            entity.Id = model.Id;
            entity.ProductVariantId = model.ProductVariantId;
            entity.Title = model.Title;
            entity.UpdatedOnUtc = model.UpdatedOnUtc;
            return entity;
        }
        public static ProductModel.ProductVariantModel ToModel (this ProductVariant entity)
        {
            var model = new ProductModel.ProductVariantModel();
            model.Description = entity.Description;
            model.Id = entity.Id;
            model.ManufacturerPartNumber = entity.ManufacturerPartNumber;
            model.Name = entity.Name;
            model.Sku = entity.Sku;
            model.ProductVariantPrice.PriceValue = entity.Price;
            model.ProductVariantPrice.Price = entity.Price.ToString();
            model.ProductVariantPrice.OldPrice = entity.OldPrice.ToString() ;
            model.ProductVariantPrice.PriceWithDiscountValue = entity.SpecialPrice.HasValue ? entity.SpecialPrice.Value : 0;
            return model;
        }
        public static ShareBuyModel ToModel (this ShareBuyItem entity)
        {
            var model = new ShareBuyModel();
            model.CreatedOnUtc=entity.CreatedOnUtc;
            model.CustomerId=entity.CustomerId;
            model.DefaultPictureId = entity.DefaultPictureId;
            model.Description=entity.Description;
            model.Deleted=entity.Deleted;
            model.Id=entity.Id;
            model.Likes = entity.ShareBuyVotes.Count;
            //product model
            model.ProductVariantId = entity.ProductVariantId;
            model.ProductName = entity.ProductVariant.Product.Name;
            model.ProductVariantModel = entity.ProductVariant.ToModel();
            model.ProductId = entity.ProductVariant.ProductId;
            model.ProductSeName = entity.ProductVariant.Product.GetSeName();
            model.Title=entity.Title;
            model.UpdatedOnUtc=entity.UpdatedOnUtc ;
  
            //sharebuy pictures model
            foreach (ShareBuyPicture item in entity.ShareBuyPictures)
            {
                model.ShareBuyPictureModels.Add(item.ToModel());
            }

            
            return model;
        }

        public static ShareBuyPictureModel ToModel(this ShareBuyPicture entity)
        {
            var model = new ShareBuyPictureModel();
            model.DisplayOrder = entity.DisplayOrder;
            model.Id = entity.Id;
            model.PictureId = entity.PictureId;
            model.ShareBuyId = entity.ShareBuyItemId;
            return model;
        }

        public static ShareBuyCommentModel ToModel(this ShareBuyComment entity)
        {
            var model=new ShareBuyCommentModel();
            model.CommentText = entity.CommentText;
            model.CreatedOn = entity.CreatedOnUtc;
            model.CustomerId = entity.CustomerId;
            model.Id = entity.Id;

            return model;
        }
    }
}